home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
10 - Networking
/
NovelNetwar
/
helpBox.c
< prev
next >
Wrap
Text File
|
1995-05-12
|
9KB
|
471 lines
// Show a friendly help box to the user.
#include "NovelNetwar.h"
static WindowPtr helpWPtr;
static TEHandle helpTEH;
static Rect helpDestRect,helpViewRect,helpVScrollRect;
static ControlHandle helpScrollControl;
static int helpNumLines;
ControlActionUPP helpScrollProc(ControlHandle theControl,int theCode);
static void helpResizeWindow(void);
void helpInit(void)
{
helpWPtr = 0L;
helpTEH = 0L;
helpScrollControl = 0L;
}
void helpStartup(void)
{
}
void helpShutDown(void)
{
if (helpTEH)
TEDispose(helpTEH);
if (helpScrollControl)
DisposeControl(helpScrollControl);
if (helpWPtr)
DisposeWindow(helpWPtr);
helpScrollControl = 0L;
helpWPtr = 0L;
helpTEH = 0L;
}
ControlActionUPP helpScrollProc(ControlHandle theControl,int theCode)
{
int scrollAmt,height;
int controlMax,controlMin,controlVal;
controlMax = GetCtlMax(theControl);
controlMin = GetCtlMin(theControl);
controlVal = GetCtlValue(theControl);
switch (theCode)
{
case inUpButton:
if (controlVal > controlMin)
{
SetCtlValue(theControl,controlVal-1);
scrollAmt = TEGetHeight(controlVal,controlVal,helpTEH);
TEScroll(0,scrollAmt,helpTEH);
}
break;
case inPageUp:
if (controlVal > controlMin)
{
scrollAmt = 1;
height = helpViewRect.bottom - helpViewRect.top;
while (controlVal-scrollAmt>controlMin && TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH)<height)
scrollAmt++;
if (scrollAmt>1 && TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH)>height)
scrollAmt--;
SetCtlValue(theControl,controlVal-scrollAmt);
TEScroll(0,TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH),helpTEH);
}
break;
case inDownButton:
if (controlVal < controlMax)
{
SetCtlValue(theControl,controlVal+1);
scrollAmt = -TEGetHeight(controlVal+1,controlVal+1,helpTEH);
TEScroll(0,scrollAmt,helpTEH);
}
break;
case inPageDown:
if (controlVal < controlMax)
{
scrollAmt = 1;
height = helpViewRect.bottom - helpViewRect.top;
while (controlVal+scrollAmt<controlMax && TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH)<height)
scrollAmt++;
if (scrollAmt>1 && TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH)>height)
scrollAmt--;
SetCtlValue(theControl,controlVal+scrollAmt);
TEScroll(0,-TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH),helpTEH);
}
break;
}
}
void DoHelpBox(int helpResWIND,int helpResTEXT,int helpResSTYL)
{
Handle helpText,helpStyle;
Cursor waitCursor;
CursHandle hCurs;
if (helpWPtr)
{
SelectWindow(helpWPtr);
return;
}
helpText = GetResource('TEXT',helpResTEXT);
helpStyle = GetResource('styl',helpResSTYL);
if (helpText == 0L || helpStyle == 0L)
{
ErrorAlert("DoHelpBox: Can't load help TEXT/STYL resources!");
helpShutDown();
goto EXITPOINT;
}
helpWPtr = GetNewWindow(helpResWIND,0L,(WindowPtr) -1L);
if (helpWPtr == 0L)
{
ErrorAlert("DoHelpBox: Can't allocate helpWPtr!");
helpShutDown();
goto EXITPOINT;
}
SetPort(helpWPtr);
CenterWindow(helpWPtr);
ShowWindow(helpWPtr);
DrawGrowIcon(helpWPtr);
TextFont(geneva);
TextSize(10);
TextFace(0);
SetRect(&helpViewRect,helpWPtr->portRect.left,helpWPtr->portRect.top,helpWPtr->portRect.right-15,helpWPtr->portRect.bottom-15);
InsetRect(&helpViewRect,5,5);
helpDestRect = helpViewRect;
helpTEH = TEStylNew(&helpDestRect,&helpViewRect);
if (helpTEH == 0L)
{
ErrorAlert("DoHelpBox: Can't allocate helpTEH!");
helpShutDown();
goto EXITPOINT;
}
(**helpTEH).lineHeight = -1;
(**helpTEH).fontAscent = -1;
SetRect(&helpVScrollRect,helpWPtr->portRect.right-15,helpWPtr->portRect.top-1,helpWPtr->portRect.right+1,helpWPtr->portRect.bottom-14);
helpScrollControl = NewControl(helpWPtr,&helpVScrollRect,"\p",TRUE,1,1,1,scrollBarProc,(long) helpTEH);
if (helpScrollControl == 0L)
{
ErrorAlert("DoHelpBox: Can't allocate helpScrollControl!");
helpShutDown();
goto EXITPOINT;
}
SetCtlMin(helpScrollControl,0);
SetCtlValue(helpScrollControl,0);
HLock(helpText);
hCurs = GetCursor(watchCursor);
if (hCurs)
{
waitCursor = **hCurs;
SetCursor(&waitCursor);
}
else
{
InitCursor();
}
TEStylInsert(*helpText,SizeResource(helpText),(StScrpHandle) helpStyle,helpTEH);
InitCursor();
HUnlock(helpText);
ReleaseResource(helpText);
ReleaseResource(helpStyle);
helpResizeWindow();
BeginUpdate(helpWPtr);
EndUpdate(helpWPtr);
EXITPOINT:
return;
}
void helpCloseWindow(WindowPtr whichWindow)
{
if (helpWPtr && helpWPtr == whichWindow)
{
helpShutDown();
EnableEditMenu();
}
}
static void helpResizeWindow(void)
{
int nLines,height;
if (helpWPtr && helpScrollControl)
{
nLines = (**helpTEH).nLines;
helpNumLines = 1;
height = (**helpTEH).viewRect.bottom - (**helpTEH).viewRect.top;
while (nLines-helpNumLines > 1 && TEGetHeight(nLines-helpNumLines,nLines-1,helpTEH) < height)
helpNumLines++;
/*
if (helpNumLines > 1 && TEGetHeight(nLines-helpNumLines,nLines-1,helpTEH) > height)
helpNumLines--;
*/
if (nLines - helpNumLines > 0)
SetCtlMax(helpScrollControl,nLines - helpNumLines);
else
SetCtlMax(helpScrollControl,0);
}
}
void helpGrowWindow(WindowPtr whichWindow,EventRecord *theEvent)
{
Rect tempRect;
long newSize;
GrafPtr oldPort;
int ctlVal,height;
if (helpWPtr && whichWindow == helpWPtr && helpScrollControl)
{
SetRect(&tempRect,165,165,32767,32767);
newSize = GrowWindow(whichWindow,theEvent->where,&tempRect);
SizeWindow(whichWindow,LoWord(newSize),HiWord(newSize),0xff);
tempRect = whichWindow->portRect;
GetPort(&oldPort);
SetPort(whichWindow);
EraseRect(&tempRect);
InvalRect(&tempRect);
SetPort(oldPort);
MoveControl(helpScrollControl,tempRect.right+1-16,tempRect.top-1);
SizeControl(helpScrollControl,16,whichWindow->portRect.bottom-whichWindow->portRect.top-13);
SetRect(&helpViewRect,helpWPtr->portRect.left,helpWPtr->portRect.top,helpWPtr->portRect.right-15,helpWPtr->portRect.bottom-15);
InsetRect(&helpViewRect,5,5);
helpDestRect = helpViewRect;
ctlVal = GetCtlValue(helpScrollControl);
if (ctlVal > 0)
height = TEGetHeight((long) (ctlVal-1),0L,helpTEH);
else
height = 0;
helpDestRect.top -= height;
(*helpTEH)->viewRect = helpViewRect;
(*helpTEH)->destRect = helpDestRect;
TECalText(helpTEH);
InitCursor();
helpResizeWindow();
}
}
void helpDragWindow(WindowPtr whichWindow,EventRecord *theEvent)
{
Rect tempRect;
if (helpWPtr && whichWindow == helpWPtr)
{
getGrayRgnRect(&tempRect);
DragWindow(whichWindow,theEvent->where,&tempRect);
}
}
void helpActivateWindow(WindowPtr whichWindow,char adFlag)
{
Rect tempRect;
GrafPtr oldPort;
if (helpWPtr && whichWindow==helpWPtr && helpScrollControl)
{
GetPort(&oldPort);
SetPort(whichWindow);
if (adFlag)
{
HiliteControl(helpScrollControl,0);
DrawControls(whichWindow);
DisableEditMenu();
}
else
{
HiliteControl(helpScrollControl,255);
EnableEditMenu();
}
DrawGrowIcon(whichWindow);
SetPort(oldPort);
}
}
OSErr helpIsWindow(WindowPtr theWPtr)
{
if (helpWPtr && theWPtr == helpWPtr)
return(noErr);
else
return(BADWINDOW);
}
void helpUpdateWindow(WindowPtr theWPtr)
{
GrafPtr oldPort;
Rect tempRect;
if (helpWPtr && theWPtr == helpWPtr)
{
GetPort(&oldPort);
SetPort(helpWPtr);
BeginUpdate(helpWPtr);
tempRect = helpWPtr->portRect;
EraseRect(&tempRect);
TEUpdate(&helpViewRect,helpTEH);
DrawGrowIcon(helpWPtr);
DrawControls(helpWPtr);
EndUpdate(helpWPtr);
SetPort(oldPort);
}
}
void helpDoContent(WindowPtr whichWindow,EventRecord *theEvent)
{
GrafPtr oldPort;
ControlHandle whichControl;
int cntlCode,oldVal,controlVal;
if (helpWPtr && whichWindow == helpWPtr && helpScrollControl)
{
GetPort(&oldPort);
SetPort(whichWindow);
GlobalToLocal(&(theEvent->where));
cntlCode = FindControl(theEvent->where,whichWindow,&whichControl);
if (cntlCode != 0)
{
if (whichControl == helpScrollControl)
{
if (cntlCode == inThumb)
{
oldVal = GetCtlValue(helpScrollControl);
TrackControl(helpScrollControl,theEvent->where,0L);
controlVal = GetCtlValue(helpScrollControl);
if (controlVal > oldVal)
TEScroll(0,-TEGetHeight(oldVal,controlVal-1,helpTEH),helpTEH);
else if (controlVal < oldVal)
TEScroll(0,TEGetHeight(controlVal,oldVal-1,helpTEH),helpTEH);
}
else
TrackControl(whichControl,theEvent->where,(ControlActionUPP) helpScrollProc);
}
}
SetPort(oldPort);
}
}